/*
GENERATE TENURE

Data 	: A1_jobb_90_13.dta
Folder 	: 
Date	: 2018-01-25

Creator		: Jonas Cederlof (JC)
Description 	: Fixing duplicates and expanding RAMS data 

Notes 		: Data only includes indiviuals eventually getting notified. 
LATEST UPDATE 	: 
*/

*===============================================================================

clear
set more		 off
cap   log close 	_all

log using 	"../log/A1_gen_tenure.log", replace 
use 		"$datapath/A1_cleaned_RAMS.dta"

{ // Program Define (Fastmax & Fastmin)
********************************************************************************
*Description: 	The purpose of the program is to speed up the max function
*		and abstain from using the much slower egen (max) command.
capture program drop fastmax fastmin

program define fastmax, rclass
	syntax varlist [if] [in] , [by(varlist)] name(string)

	tempvar maxvar
	clonevar `maxvar' = `varlist'

	if  "`by'" != "" {
		bys `by' : 	replace `maxvar' = max( `maxvar'[_n-1], `maxvar') 
		bys `by' : 	gen  	`name'   =  `maxvar'[_N] 
	}
	else {
					replace `maxvar' = max( `maxvar'[_n-1], `maxvar') 
					gen  	`name'   = `maxvar'[_N] 
}
end

program define fastmin, rclass
	syntax varlist [if] [in] , [by(varlist)] name(string)

	tempvar maxvar
	clonevar `maxvar' = `varlist'

	if  "`by'" != "" {
		bys `by' : 	replace `maxvar' = min( `maxvar'[_n-1], `maxvar') 
		bys `by' : 	gen  	`name'   =  `maxvar'[_N] 
	}
	else {
					replace `maxvar' = min( `maxvar'[_n-1], `maxvar') 
					gen  	`name'   = `maxvar'[_N] 
}
end



}
*

{ // Create a employment spell indicator within each firm
********************************************************************************
* The spell indicator "nrspell" displays the n:th spell at a particular firm.
* A new spell occurs if the indviual has not been observred at the firm for more
* than 18 months. These types of "periods of absence" could be parental-leave
* and other things that should be indluced in tenure. Using the variable 
* "nrspell" in the final as an if command we can check the robustness by forc-
* ing nrspell==1. This turns out to be true for about  97% of the spells which
* would indicate that these "holes" are not long parental-leave stuff?


* Indicator for the very first observed month working at firm or if the indiviual 
* not having contact with the firm for more than 18 months.
* Note the very last if statement work[_n-1]==1 is becuse I have sorted on work.
bys persid firmid work 	(date) : gen 	 nrspell = 1  	 		if [_n==1] & work==1 | date[_n]  - date[_n-1] >18 & work[_n]==1 & work[_n-1]==1
bys persid firmid 	(date) : replace nrspell = sum(nrspell)

* When an individual has mutliple spells at an employer, some months of the new
* spell might be wrongly attributed to the former one if she does not start her 
* new spell in Janurary. Hence, I replace these months with missing values for
* nrspell.
fastmax nrspell , by(persid firmid year) name(year_maxspellnr)
replace nrspell = . if nrspell!=year_maxspellnr
drop year_maxspellnr


* For periods where we are lacking data (i.e. one year is missing due to e.g.
* parental-leave) we want to add 12 months to tenure. Hence, I indicate these
* periods with a 12
bys persid firmid 	nrspell   (date) : gen 	 	addmonths = 12  	 if  year[_n]  - year[_n-1]==2  
* Filling in subsequent observations in order to be able to add to tenure for 
* all months following the "gap".
bys persid firmid 	nrspell   (date) : replace  addmonths = max(addmonths[_n-1],addmonths[_n])
bys persid firmid 	nrspell   (date) : replace  addmonths = 0 		if addmonths==.
}
*
{ // Generete: Meassures of tenure
********************************************************************************

*===============================================================================

* Tenure0: 	Inclduing non-work months (work==0) and reseting tenure clock to 1 if 
* 		new spell (i.e. not registered as working for >18 months). Does not
*		add 12 months when the there is a one year "period of absence" in
* 		the data.
*===============================================================================

bys persid firmid nrspell (date) : gen tenure0 = _n 		if nrspell!=.


*===============================================================================

* Tenure1: 	Inclduing non-work months (work==0) and reseting tenure clock to 1 if 
* 		new spell (i.e. not registered as working for >18 months)
*===============================================================================

bys persid firmid nrspell (date) : gen tenure1 = _n + addmonths  if nrspell!=.
* Example : list if persid==28051, sepby(firmid)


*===============================================================================
* Tenure2: 	Including non-work months (work==0) and NOT reseting tenure clock.
* 		We start counting tenure in the new spell and add the number of 
* 		months of tenure aquired during previous employment spells. It also
* 		adds 12 months if the period of absence is a year.
*===============================================================================

bys persid firmid   (date) : gen tenure2 = sum(1) + addmonths  


*===============================================================================
* Tenure3: 	Including non-work months (work==0) if they do not occur at the start
*		or end of the employment period (as calculated by nrspell). 
*		Does NOT reset tenure clock but adds the number of months from 
*		previous emp spell. But in contrast to Tenure2 it adds the number 
*		of months from the last period of work==1 from another employment 
* 		spell.
*===============================================================================

*Mark periods of working (where work==0 is allowed WITHIN the spell)
bys persid firmid nrspell work (date) : gen 	temp_startspell = date[_n==1]  	if work==1
bys persid firmid nrspell work (date) : gen 	temp_endspell 	= date[_N] 	if work==1 

* For the last (or only spell) make sure to keep counting months after the work 
* zero months in the last spell. This in order to  tenure 4 for impute _merge==2 
* (after dec2013) observations.
fastmax nrspell, by(persid firmid) name(maxspell)
bys persid firmid  (date) : replace temp_endspell = date[_N]  if nrspell==maxspell

fastmax temp_start	, by(persid firmid nrspell) name(startdate_tenure3)
fastmax temp_end	, by(persid firmid nrspell) name(enddate_tenure3)

gen marktenure3  =  	date>=startdate_tenure3 & date<=enddate_tenure3

*Summing marked periods
bys persid firmid   (date) : gen tenure3 = sum(marktenure3) 


replace tenure3 =. if tenure3==0

drop temp_start temp_end startdate_tenure3 enddate_tenure3 marktenure3


*===============================================================================
* Tenure4: 	Excluding non-work months (work==0) and does NOT reset tenure clock.
*		I.e. only sums up number of months worked at the firm in total. 
*===============================================================================

bys persid firmid  (date)  : gen tenure4 = sum(work)
replace tenure4 =. if tenure4==0


}
*
{ // Generate: Startdate for each meassure of tenure
********************************************************************************
* As some meassures of tenure does not reset the tenure clock, the start date
* will vary depending on this. In other words, startdate0==startdate1 as both 
* of them reset the tenure clock. startdate2==startdate3==startdate4 as non of
* them reset tenure clock and hence have the same first registration of work.
* If nrspell==1 all startdates are equal.

*Startdate 0 and 1 (for Tenure 0 and 1 with reset of tenure clock)
forvalues i = 0(1)1{
	bys persid firmid nrspell (date) : gen 	start`i'= date 	  if tenure`i'==1
	fastmax start`i', by(persid firmid nrspell) name(startdate`i')
	drop start`i'
}
*startdate 2,3,4 with NO reset of tenure clock 
	bys persid firmid 		  (date) : gen 	start2 = date  	if tenure2==1 
	fastmax start2, by(persid firmid) name(startdate2)
	clonevar startdate3 = startdate2
	clonevar startdate4 = startdate2
	
format startdate* %tm	
drop   start2

}
*

*===============================================================================
* Tenure5: 	Months at a firm irrespective of work being ==1 or ==0.
*		I.e months since startdate
*===============================================================================

gen tenure5 = date - startdate4 

{ // Labeling and adding notes to tenure variabels
********************************************************************************
lab var tenure0 	"Tenure: inc. non-work & reseting tenure clock if >18 months of absence"
lab var tenure1 	"Tenure: inc. non-work & reseting tenure clock if >18 months of absence.  If <1 year, adds 12 months"
lab var tenure2 	"Tenure: inc. non-work & NO reset of tenure clock. If absence  If <1 year, adds 12 months"
lab var tenure3 	"Tenure: inc. non-work if within spell & NO reset of tenure clock"
lab var tenure4 	"Tenure: Number of months worked at firm (i.e sum(work))"
lab var tenure5 	"Tenure: Months since startdate (i.e months since first work==1 at firm))"


note 	tenure0 : 	Tenure inc. non-work & reseting tenure clock if >18 months of absence.
note 	tenure1 :  	Tenure inc. non-work & reseting tenure clock if >18 months of absence. If there is an absence if one year from firm it adds 12 months.
note 	tenure2 :  	Tenure inc. non-work & NO reset of tenure clock. If there is an absence if one year from firm it adds 12 months.
note 	tenure3 : 	Tenure inc. non-work if within spell & NO reset of tenure clock but adds number of months aquired in previous emp. spells.
note 	tenure4 : 	Tenure exc. non-work and NO reset of tenure clock. CODE bys persid firmid (date) : gen tenure4 = sum(work)

}
*


*keeping variabels (dropping startdate)
keep persid date firmid tenure*  startdate*

compress
save "$datapath/A2_tenure.dta", replace


log close
